package com.alex.seo.service.campaign.executor.publishing;

import org.springframework.beans.factory.annotation.Required;
import org.springframework.transaction.annotation.Transactional;

import com.alex.seo.contentsource.NoContentException;
import com.alex.seo.contentsource.dto.ContentDTO;
import com.alex.seo.contentsource.exeption.ContentSourceException;
import com.alex.seo.service.publishing.PublishingService;
import com.alex.seo.service.publishing.PublishingServiceException;
import com.alex.seo.service.scheduler.Job;
import com.alex.seo.service.scheduler.JobExecutionException;

@Transactional
public class PublishingJob implements Job<PublishingJobContext> {

	private PublishingService publishingService;
	
	@Required
	public void setPublishingService(PublishingService publishingService) {
		this.publishingService = publishingService;
	}
	
	@Override
	public void execute(PublishingJobContext context) throws JobExecutionException {
		try {
			ContentDTO contentDTO = context.getContentSource().getContent();
			publishingService.createPost(context.getSite().getId(), contentDTO);
		} catch (PublishingServiceException | ContentSourceException | NoContentException e) {
			throw new JobExecutionException(e);
		}
	}
}
